package com.developer.platform.application.query;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.developer.platform.application.query.assembler.RoleAssembler;
import com.developer.platform.application.query.dto.ResponseModel;
import com.developer.platform.application.query.dto.RoleDto;
import com.developer.platform.application.query.dto.RoleListDto;
import com.developer.platform.application.query.dto.UserRoleDto;
import com.developer.platform.domain.Role;
import com.developer.platform.domain.UserRole;
import com.developer.platform.infrastructure.data.mapper.RoleMapper;
import com.developer.platform.infrastructure.data.mapper.UserRoleMapper;
import com.developer.platform.infrastructure.enums.StatusEnum;
import com.veeker.core.bean.Pageable;
import java.util.List;
import java.util.Objects;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2020-11-09
 */
@Component
public class RoleQueryManager {

    private final RoleMapper roleMapper;
    private final RoleAssembler roleAssembler;
    private final UserRoleMapper userRoleMapper;

    public RoleQueryManager(RoleMapper roleMapper,
                            RoleAssembler roleAssembler,
                            UserRoleMapper userRoleMapper) {
        this.roleMapper = roleMapper;
        this.roleAssembler = roleAssembler;
        this.userRoleMapper = userRoleMapper;
    }

    /**
     *  分页查询角色
     *
     * @author ：qiaoliang
     * @param name :    角色名称
     * @param status :  角色状态
     * @param pageable :    分页信息
     * @return com.microorganism.platform.application.query.dto.ResponseModel<com.microorganism.platform.application.query.dto.RoleListDto>
     * @date 2020-11-09 15:53
     */
    public ResponseModel<RoleListDto> list(String name, StatusEnum status, Pageable pageable){
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StrUtil.isNotBlank(name), Role::getName, name);
        queryWrapper.eq(Objects.nonNull(status), Role::getStatus, status);
        return roleAssembler.toRoleListDto(
                roleMapper.selectPage(new Page<>(pageable.getPageNumber(), pageable.getPageSize()), queryWrapper));
    }

    /**
     *  通过状态查询角色集合
     *
     * @author ：qiaoliang
     * @param status : 状态
     * @return java.util.List<com.microorganism.platform.application.query.dto.RoleDto>
     * @date 2020-11-09 15:56
     */
    public List<RoleDto> list(StatusEnum status){
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getStatus, status);
        queryWrapper.orderByAsc(Role::getSort);
        return roleAssembler.toRoleDto(roleMapper.selectList(queryWrapper));
    }


    /**
     * 查询用户角色
     *
     * @author ：qiaoliang
     * @param userId : 用户ID
     * @return java.util.List<java.lang.Long>
     * @date 2020-11-05 11:45
     */
    public UserRoleDto userRoleByUserId(long userId){
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId, userId);
        List<UserRole> userRoles = userRoleMapper.selectList(queryWrapper);
        List<RoleDto> roleDtoList = list(StatusEnum.Y);
        return roleAssembler.toUserRoleDto(userRoles, roleDtoList);
    }

}
